package cn.sytton.taffe.web.flw.candidate;

import cn.hutool.core.collection.CollectionUtil;
import cn.sytton.taffe.framework.util.SpringContextUtil;
import cn.sytton.taffe.web.sys.entity.DeptUserEntity;
import cn.sytton.taffe.web.sys.entity.UserEntity;
import cn.sytton.taffe.web.sys.mapper.DeptUserMapper;
import cn.sytton.taffe.web.sys.mapper.UserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取审批人表达式
 *
 * @author skyrock
 */
@Slf4j
@Service("flowCandidateUsers")
public class User {

    @Resource
    private DeptUserMapper deptUserMapper;
    @Resource
    private UserMapper userMapper;


    /**
     * 获取某部门的审批人列表
     * @param deptIds
     * @return
     */
    public List<String> getByDept(String... deptIds) {
        List<String> userIds = new ArrayList<>();
        for(String deptId : deptIds) {
            List<DeptUserEntity> users = deptUserMapper.findByDeptId(deptId);
            userIds.addAll(users.stream().map(UserEntity::getUserId).collect(Collectors.toList()));
        }
        return userIds;
    }

    /**
     * 获取某部门岗位内的审批人列表
     * @param deptId
     * @param positionId
     * @return
     */
    public List<String> getByDeptPosition(String deptId, String positionId) {
        List<DeptUserEntity> users = deptUserMapper.findByDeptId(deptId);
        return users.stream().filter(u -> u.getPositionId().equals(positionId)).map(UserEntity::getUserId).collect(Collectors.toList());
    }

    /**
     * 获取某岗位内审批人
     * @param positionIds
     * @return
     */
    public List<String> getByPosition(String... positionIds) {
        List<String> userIds = new ArrayList<>();
        for(String positionId : positionIds) {
            List<UserEntity> users = userMapper.selectList(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getPositionId, positionId));
            userIds.addAll(users.stream().map(UserEntity::getUserId).collect(Collectors.toList()));
        }
        return userIds;
    }

    /**
     * 获取申请人部门的审批人列表
     * @param applyUserId
     * @return
     */
    public List<String> getByMyDept(String applyUserId) {
        List<DeptUserEntity> deptUsers = deptUserMapper.selectList(new LambdaQueryWrapper<DeptUserEntity>().eq(DeptUserEntity::getUserId, applyUserId));
        return getByDept(deptUsers.stream().map(DeptUserEntity::getDeptId).toArray(String[]::new));
    }

    /**
     * 获取申请人部门某岗位审批人列表
     * @param applyUserId
     * @param positionId
     * @return
     */
    public List<String> getByMyDeptPosition(String applyUserId, String positionId) {
        List<String> deptUserIds = getByMyDept(applyUserId);
        List<String> positionUserIds = getByPosition(positionId);
        return Lists.newArrayList(CollectionUtil.intersection(deptUserIds, positionUserIds));
    }

    /**
     * 从特定bean内获取审批人
     * @param processKey
     * @param taskKey
     * @param applyUserId
     * @return
     */
    public List<String> getByBean(String processKey, String taskKey, String applyUserId, String beanId) {
        if (SpringContextUtil.containsBean(beanId)) {
            try{
                IUser service = SpringContextUtil.getBean(beanId);
                return service.getApproveUserIds(processKey, taskKey, applyUserId);
            } catch (Exception e) {
                log.error("未获取审批人, processKey=>"+processKey+"taskKey=>"+taskKey+"applyUserId=>"+applyUserId, e);
                return null;
            }
        } else {
            log.error("未获取审批人 beanId["+beanId+"]不存在 ,processKey=>"+processKey+"taskKey=>"+taskKey+"applyUserId=>"+applyUserId);
            return null;
        }
    }
}
